package cn.shutdown.redis;

import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;

import java.util.ArrayList;
import java.util.List;

/**
 * 通过Factory创建JedisCluster
 * @author jiangwujie
 * @date 2021/11/8
 */
public class BloomFilterDemo1 {

    public static void main(String[] args) throws Exception {
        List<String> redis = new ArrayList<String>();
        String address = "10.4.7.212:6410,10.4.7.213:7410,10.4.7.213:6411,10.4.7.214:7411,10.4.7.214:6412,10.4.7.212:7412";
        String[] addressArr = address.trim().split(",");
        for (String addressStr : addressArr) {
            redis.add("redis://" + addressStr);
        }
        Config config = new Config();
        ClusterServersConfig clusterServersConfig = config.useClusterServers();
        clusterServersConfig.setNodeAddresses(redis);
        clusterServersConfig.setPassword("2651080c6814a4a9d62da69a12f962b6");
        RedissonClient redisson = Redisson.create(config);
        RBloomFilter<String> bloomFilter = redisson.getBloomFilter("testkey1");
        //预期插入量
        //过滤器的错误率在0-1之间，如果要设置0.1%，则应该是0.001。该数值越接近0，内存消耗越大，对cpu利用率越高
        bloomFilter.tryInit(100000000L, 0.03);

        //向布隆过滤器中添加元素
        for (int i = 1; i <= 10000; i++) {
            bloomFilter.add("user" + i);
        }

        //判断元素是否存在于布隆过滤器中
        System.out.println("========>"+bloomFilter.contains("user888"));;
        System.out.println("========>"+bloomFilter.contains("user8888"));;
        System.out.println("========>"+bloomFilter.contains("user9999"));;
        System.out.println("========>"+bloomFilter.contains("user10001"));;
        System.out.println("========>"+bloomFilter.contains("user10002"));;
        redisson.shutdown();
    }
}
